package com.zj.osm.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.zj.osm.R;
import com.zj.osm.bean.PoleAdapterBean;
import com.zj.osm.entity.greendao.Equipment;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.view.map.line.LineActivity;
import com.zj.osm.view.map.map.EquActivity;
import java.util.List;
/**
 * BottomActivity中的Twofragment中的本地项目的适配器
 */
public class PoleAdapter2 extends BaseAdapter {
    private List<PoleAdapterBean> list;
    private Context context;
    private boolean IS_CHECK;//是否可以点击

    public PoleAdapter2(List<PoleAdapterBean> poleFragmentBeansData, Context context) {
        this.list = poleFragmentBeansData;
        this.context = context;
    }

    private static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        // listAdapter.getCount()返回数据项的数目
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_material2, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //判断当前item是点位还是线，如果是点位则根据是电缆井还是环网箱显示不同内容
        Object object = list.get(position).getPointOrLine();
        if (object instanceof Point){
            Point point = (Point) object;
            if ("电缆井".equals(point.getEquipType_name())){
                viewHolder.leixing_tv.setText("电缆井编号："+point.getMark());
                viewHolder.tv_xuanxing.setText("电缆井尺寸："+point.getDljCC());
            }else {
                viewHolder.leixing_tv.setText("环网箱编号："+point.getMark());
                viewHolder.tv_xuanxing.setText("资源规格："+point.getReSpecfs());
            }
        }
        PoleAdapterBean points = list.get(position);
        //viewHolder.leixing_tv.setText(points.getTypeName());
        viewHolder.statu_tv.setText(points.getStatu());
        //进行listView的选型坐适配器负值

        if (list.get(position).getEquipmentList().size() > 0) {
            XuanAdapter xuanAdapter = new XuanAdapter(list.get(position).getEquipmentList());
            viewHolder.mListView.setAdapter(xuanAdapter);
            setListViewHeightBasedOnChildren(viewHolder.mListView);
            xuanAdapter.notifyDataSetChanged();
            viewHolder.qiehuanIv.setBackgroundResource(R.drawable.xialas);
            viewHolder.qiehuanIv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //viewHolder.mListView.setVisibility(View.GONE);
            IS_CHECK = true;
        } else {
            viewHolder.qiehuanIv.setBackgroundResource(R.drawable.shangla);
            viewHolder.qiehuanIv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //viewHolder.mListView.setVisibility(View.GONE);
            IS_CHECK = false;
        }
        final boolean[] flag = {true};
        if (IS_CHECK) {
            viewHolder.qiehuanIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag[0]) {
                        viewHolder.qiehuanIv.setBackgroundResource(R.drawable.shangla);
                        viewHolder.qiehuanIv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        viewHolder.mListView.setVisibility(View.VISIBLE);
                    } else {
                        viewHolder.qiehuanIv.setBackgroundResource(R.drawable.xialas);
                        viewHolder.qiehuanIv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        viewHolder.mListView.setVisibility(View.GONE);
                    }
                    flag[0] = !flag[0];
                }
            });
        }
        if (points.isPoint()) {
            viewHolder.toLinOrPointActivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, EquActivity.class);
                    intent.putExtra("point", (Point) points.getPointOrLine());
                    context.startActivity(intent);
                }
            });

        } else {
            viewHolder.toLinOrPointActivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LineActivity.class);
                    intent.putExtra("line", (Line) points.getPointOrLine());
                    context.startActivity(intent);
                }
            });

        }


        return convertView;
    }

    private static class ViewHolder {
        //<!--pole_id pole_type pole_lijiu pole_state-->

        View rootView;
        TextView statu_tv;
        TextView leixing_tv;
        ListView mListView;
        ImageView qiehuanIv;
        TextView toLinOrPointActivity;
        TextView tv_xuanxing;

        ViewHolder(View rootView) {
            this.rootView = rootView;
            this.statu_tv = rootView.findViewById(R.id.statu_tv);
            this.leixing_tv = rootView.findViewById(R.id.leixing_tv);
            this.mListView = rootView.findViewById(R.id.xuanxing_lv);
            this.qiehuanIv = rootView.findViewById(R.id.qiehuanIv);
            this.toLinOrPointActivity = rootView.findViewById(R.id.toLinOrPointActivity);
            this.tv_xuanxing = rootView.findViewById(R.id.tv_xuanxing);

        }

    }

    class XuanAdapter extends BaseAdapter {
        List<Equipment> equipmentData;

        XuanAdapter(List<Equipment> equipmentData) {
            Log.e("woqu", "heheheheh");
            this.equipmentData = equipmentData;
        }

        @Override
        public int getCount() {
            return equipmentData.size();
        }

        @Override
        public Object getItem(int position) {
            return equipmentData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolders viewHolders;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.layout_xuanxin_adapter_item, null);
                viewHolders = new ViewHolders(convertView);
                convertView.setTag(viewHolders);
            } else
                viewHolders = (ViewHolders) convertView.getTag();
            viewHolders.NameTv.setText(equipmentData.get(position).getName());
            viewHolders.subTv.setText(equipmentData.get(position).getThreeName());
            return convertView;
        }

        class ViewHolders {
            TextView NameTv;
            TextView subTv;

            ViewHolders(View view) {
                NameTv = view.findViewById(R.id.equ_tv_name);
                subTv = view.findViewById(R.id.equ_ed_lever);

            }
        }

    }
}
